package com.dajingling.controller;

import com.dajingling.bean.*;
import com.dajingling.mvc.ResponseBody;
import com.dajingling.service.ExpressService;
import com.dajingling.util.DateFormatUtil;
import com.dajingling.util.JSONUtil;
import com.dajingling.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/admin/views/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String,Integer>> list = ExpressService.console();
        Message msg = new Message();
        if (list.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(list);
        String json = JSONUtil.toJSON(msg);
        System.out.println("控制台："+json);
        return json;
    }

    @ResponseBody("/admin/views/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的索引
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<Express> list = ExpressService.findAll(true,offset,pageNumber);
        List<BootStrapTableExpress> list1 = new ArrayList<>();

        for (Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(), inTime, outTime, status, e.getSysPhone());
            list1.add(e2);
        }

        List<Map<String,Integer>> console = ExpressService.console();
        int total = console.get(0).get("data1_size");
        //4.把集合封装为bootstrap-table(前端的表格)能识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println("快递列表："+json);
        return json;
    }

    @ResponseBody("/admin/views/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        UserAdmin xuser = UserUtil.getWxUser(request.getSession());
        String phone = "";
        if (xuser==null){
            phone = "后台录入";
        }else {
            phone = xuser.getUserPhone();
        }
        Express e = new Express(number, userName, userPhone, company, phone);
        boolean flag =  ExpressService.insert(e);
        Message msg = new Message();
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递录入成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("快递录入："+json);
        return json;
    }
    @ResponseBody("/admin/views/express/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Express express = ExpressService.findByNumber(number);
        Message msg = new Message();
        if (express == null){
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(express);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("查询快递："+json);
        return json;
    }

    @ResponseBody("/admin/views/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUserName(userName);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);
        boolean flag = ExpressService.update(id,newExpress);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("修改快递："+json);
        return json;
    }

    @ResponseBody("/admin/views/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = ExpressService.delete(id);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("删除快递："+json);
        return json;
    }
}
